package com.example.redisstudy.concurrent;

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.*;

/**
 * @Classname futureTask
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/9/19 下午2:58
 * @Created by ligang
 */
public class futureTask {
//    public static void main(String[] args) throws InterruptedException, ExecutionException {
//        Callable<String> callable = () -> {
//            System.out.println("Entered Callable");
//            Thread.sleep(2000);
//            return "Hello from Callable";
//        };
//
//        FutureTask<String> futureTask = new FutureTask<>(callable);
//        Thread thread = new Thread(futureTask);
//        thread.start();
//
//        System.out.println("Do something else while callable is getting executed");
//        System.out.println("Retrieved: " + futureTask.get());
//    }

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        Callable<String> callable = () -> {
            System.out.println("Entered Callable");
            Thread.sleep(2000);
            return "Hello from Callable";
        };

        System.out.println("Submitting Callable");
        Future<String> future = executor.submit(callable);

        System.out.println("Do something else while callable is getting executed");
        System.out.println("Retrieved: " + future.get());

        executor.shutdown();

        Instant start = Instant.now();
        Instant finish = Instant.now();
        long millis = Duration.between(start, finish).toMillis();
        System.out.println(millis);
    }
}
